using System;
using System.Collections.Generic;
using System.Text;

using phorm;
using phorm.application;
using phorm.config;
using phorm.database.core;
using phorm.database.mssql;
using phorm.model;
using phorm.utils;

namespace TestPhorm
{
    /// <summary>
    /// First test case:
    /// Sets up config parameters, database, and tables. Creates one of each
    /// type of object in the Model. Attempts a save to test unchanged columns.
    /// Tests the adding of a ForeignKey object. Pauses to see changes in
    /// InsertOnlyColumn versus AutoUpdateColumn. Attempts to get an XML and
    /// output it to the screen. Tests all() method for a many-to-many
    /// relationship. Attempts to remove a many-to-many relationship. Tests
    /// initWithId function. Attempts to empty and drop tables.
    /// </summary>
    class Test1
    {
        /// <summary>
        /// Runs this test case.
        /// </summary>
        public static void runTest()
        {
            PhormParams phormParams = new PhormParams();
            phormParams.dbType = "mssql";
            phormParams.dbHost = "localhost";
            phormParams.dbUsername = "sa";
            phormParams.dbPassword = "password";
            phormParams.dbName = "bookStore";
            PhormConfig.phormParams = phormParams;

            ApplicationMSSQL.setup();

            Author author = new Author();
            author.AuthorFirstName.value = "Pogo";
            author.Password.value = "testpass";
            author.save();
            author.save();

            Publisher publisher = new Publisher();
            publisher.PublisherName.value = "Pogo Publishing House";
            publisher.save();

            Book book1 = new Book();
            book1.BookName.value = "Pogo and the Sea-Goats: An Autobiography";
            book1.ISBN.value = 12349592;
            book1.Price.value = 59.95;
            book1.Authors.add(author);
            book1.PublisherID.add(publisher);
            book1.save();

            Book book2 = new Book();
            book2.BookName.value = "Pogo: The Last Unicorn?";
            book2.ISBN.value = 12349593;
            book2.Authors.add(author);
            book2.Price.value = 59.95;
            book2.PublisherID.add(publisher);
            book2.RelatedBooks.add(book1);
            Console.WriteLine("\n--------------------------------------------------------------------------------");
            Console.Write("PAUSING TO SEE DIFFERENT LASTMOD TIMESTAMP...Press any key to continue...");
            Console.WriteLine("\n--------------------------------------------------------------------------------");
            Console.ReadKey();
            book2.save();
            book2.save();

            book1.BookName.value = "New Title";
            book1.save();

            Console.WriteLine("\n--------------------------------------------------------------------------------");
            List<DatabaseRecord> collection = new List<DatabaseRecord>();
            collection.Add(book2);
            XMLUtils.collectionToXML(collection, 0);
            Console.WriteLine("\n--------------------------------------------------------------------------------\n");

            book1.Authors.all();
            book2.RelatedBooks.all();

            book1.Authors.remove(author);

            Book book3 = new Book();
            book3.initWithId(book1.BookID.value);

            ApplicationMSSQL.emptyTables();
            ApplicationMSSQL.dropTables();

            Console.Write("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}
